home *** CD-ROM | disk | FTP | other *** search
/ Young Minds / Young Minds Interactive CD-ROM.ISO / spacewar / plinit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-31  |  7.3 KB  |  274 lines

  1. /*
  2.  * Spacewar - routine to initialize a player into the universe
  3.  *          - terminal type
  4.  *          - craft name
  5.  *          - craft structure and universe entry
  6.  *
  7.  * Copyright 1985 obo Systems, Inc.
  8.  * Copyright 1985 Dan Rosenblatt
  9.  */
  10.  
  11. #ifndef VMS
  12. #include <sys/types.h>
  13. #include <dbm.h>
  14. #else /* BSD SYSIII SYSV */
  15. #include <types.h>
  16. #include "dbm.h"
  17. #endif /* VMS */
  18. #include "spacewar.h"
  19. #include "universe.h"
  20. #include "login.h"
  21. #include "sys.h"
  22. #include "crft.h"
  23. #include "obj.h"
  24. #include "aln.h"
  25. #include "torp.h"
  26. #include "build.h"
  27.  
  28. extern char *malloc(),*tgetstr(),*lckmsg();
  29. extern int numpling;
  30.  
  31. int plinit(plogin)
  32. register struct login *plogin;
  33. {
  34.     char trmbuf[2048],trmcap[128],*ptrmcap,*pcm,*pcl,*pce,*pso,*pse;
  35.     char buf[80+1],*s,*so,*se;
  36.     register struct universe *puniv;
  37.     register struct crft *pcrft;
  38.     struct crftkey getcrkey;
  39.     struct crft getcrdat;
  40.     struct syskey getskey;
  41.     datum dbmkey,dbmdata;
  42.     int i;
  43.  
  44. #ifdef DEBUG
  45.     DBG("plinit(#%d/%s)\n",plogin-loginlst,plogin->ln_name);
  46. #endif
  47.  
  48.     if (!strcmp(plogin->ln_input,".")) goto noplay;
  49.  
  50.     /********************/
  51.     /* no terminal type */
  52.     /********************/
  53.     if (!plogin->ln_term) {
  54.  
  55.         /* get terminal type from user input */
  56.         if (!plogin->ln_input[0]) {
  57. getterm:    output(plogin,'C',0,"\nWhat (termcap) terminal type>");
  58.         output(plogin,0,0,0);
  59. #ifdef DEBUG
  60.         VDBG("plinit return\n");
  61. #endif
  62.         return(0);
  63.         }
  64.         plogin->ln_rvslh = 0;
  65.         switch(plogin->ln_input[0]) {
  66.         case '.':
  67.             so = "so"; se = "se";
  68.             plogin->ln_rvslh = 1;
  69.             strcpy(plogin->ln_input,plogin->ln_input+1);
  70.             break;
  71.         case ',':
  72.             so = "us"; se = "ue";
  73.             strcpy(plogin->ln_input,plogin->ln_input+1);
  74.             break;
  75.         case ';':
  76.             so = "us"; se = "ue";
  77.             plogin->ln_rvslh = 1;
  78.             strcpy(plogin->ln_input,plogin->ln_input+1);
  79.             break;
  80.         default:
  81.             so = "so"; se = "se";
  82.             break;
  83.         }
  84.         if (tgetent(trmbuf,plogin->ln_input) != 1) goto getterm;
  85.  
  86.         /* get necessary capabilities */
  87.         ptrmcap = trmcap;
  88.         if (!(pcm=tgetstr("cm",&ptrmcap)) ||
  89.         !(pcl=tgetstr("cl",&ptrmcap)) ||
  90.         !(pce=tgetstr("ce",&ptrmcap)) ||
  91.         !(pso=tgetstr(so,&ptrmcap)) ||
  92.         !(pse=tgetstr(se,&ptrmcap))) {
  93.         output(plogin,'C',0,
  94.         "\nMissing minimum necessary terminal capabilities\n");
  95.         goto noplay;
  96.         }
  97.  
  98.         /* save terminal type and capabilities */
  99.         if (!(plogin->ln_term = malloc((unsigned)strlen(plogin->ln_input)+1)) ||
  100.         !(plogin->ln_tcm = malloc((unsigned)strlen(pcm)+1)) ||
  101.         !(plogin->ln_tcl = malloc((unsigned)strlen(pcl)+1)) ||
  102.         !(plogin->ln_tce = malloc((unsigned)strlen(pce)+1)) ||
  103.         !(plogin->ln_tso = malloc((unsigned)strlen(pso)+1)) ||
  104.         !(plogin->ln_tse = malloc((unsigned)strlen(pse)+1))) {
  105.         perror("plinit: out of memory for termcaps");
  106.         plogin->ln_term = NULL;
  107.         plogin->ln_tcm = NULL;
  108.         plogin->ln_tcl = NULL;
  109.         plogin->ln_tce = NULL;
  110.         plogin->ln_tso = NULL;
  111.         plogin->ln_tse = NULL;
  112.         goto noplay;
  113.         }
  114.         strcpy(plogin->ln_term,plogin->ln_input);
  115.         plogin->ln_input[0] = NULL;
  116.         strcpy(plogin->ln_tcm,pcm);
  117.         strcpy(plogin->ln_tcl,pcl);
  118.         strcpy(plogin->ln_tce,pce);
  119.         strcpy(plogin->ln_tso,pso);
  120.         strcpy(plogin->ln_tse,pse);
  121.     }
  122.  
  123.     /*********/
  124.     /* craft */
  125.     /*********/
  126.  
  127.     /* prompt for ship name */
  128.     if (!plogin->ln_input[0]) {
  129.         output(plogin,'C',0,"\nWhat ship>");
  130.         output(plogin,0,0,0);
  131. #ifdef DEBUG
  132.         VDBG("plinit return\n");
  133. #endif
  134.         return(0);
  135.     }
  136.  
  137.     /* get craft */
  138.     plogin->ln_input[sizeof(plogin->ln_crft)-1] = NULL;
  139.     binit((char *)&getcrkey,sizeof(getcrkey));
  140.     getcrkey.cr_crftkey = CRAFT;
  141.     strcpy(getcrkey.cr_plyr,plogin->ln_name);
  142.     strcpy(getcrkey.cr_name,plogin->ln_input);
  143.     dbmkey.dptr = (char *)&getcrkey;
  144.     dbmkey.dsize = sizeof(getcrkey);
  145.     dbmdata = fetch(dbmkey);
  146.     if (!dbmdata.dptr) {
  147.         sprintf(buf,"\n'%s' - no such ship\n",plogin->ln_input);
  148.         output(plogin,'C',0,buf);
  149.         goto noplay;
  150.     }
  151.     binit((char *)&getcrdat,sizeof(getcrdat));
  152.     bcopy((char *)&getcrdat,dbmdata.dptr,dbmdata.dsize);
  153.  
  154.     /* must have a hull */
  155.     if (!getcrdat.cr_htyp) {
  156.         sprintf(buf,"\n'%s' - has no hull\n",plogin->ln_input);
  157.         output(plogin,'C',0,buf);
  158.         goto noplay;
  159.     }
  160.  
  161.     /* must be room in the universe */
  162.     for (puniv=univlst+MAXOBJ+MAXALN;puniv < univlst+MAXUNIVERSE;++puniv)
  163.         if (!puniv->uv_type)
  164.         break;
  165.     if (puniv >= univlst+MAXUNIVERSE) {
  166.         output(plogin,'C',0,"\nSorry, the universe is temporarily full\n");
  167.         goto noplay;
  168.     }
  169.  
  170.     /* must not be locked */
  171.     if (s=lckmsg()) {
  172.         output(plogin,'C',0,"\n");
  173.         output(plogin,'C',0,s);
  174.         output(plogin,'C',0,"\n");
  175.         goto noplay;
  176.     }
  177.  
  178.  
  179.     /**********************************/
  180.     /* put player/craft into universe */
  181.     /**********************************/
  182.  
  183.     /* fill in craft data */
  184.     pcrft = crftlst + (plogin - loginlst);    /* 1to1 correspondence */
  185.     *pcrft = getcrdat;
  186.  
  187.     /* if first time playing, pick a pstn near the object */
  188.     if (pcrft->cr_dock.ip_ofst == -1) {
  189.         pcrft->cr_dock.ip_ptr = NULL;
  190.         i = RANDOM(MAXOBJ-1) + 1; /* 1:MAXOBJ-1 */
  191.         vcopy(pcrft->cr_pstn,univlst[i].uv_pstn);
  192.         pcrft->cr_pstn[0] =
  193.         ADD(pcrft->cr_pstn[0],FLOAT(RANDOM(2000)+500));
  194.         pcrft->cr_pstn[1] =
  195.             ADD(pcrft->cr_pstn[1],FLOAT(RANDOM(2000)+500));
  196.         pcrft->cr_vang = DIV(PI,4.);
  197.         pcrft->cr_vdst =
  198.         INT(DIV(VANGVDST,SQUARE(pcrft->cr_vang)));
  199.         
  200.         /* point so that facing the object */
  201.         vdiff(univlst[i].uv_pstn,pcrft->cr_pstn,pcrft->cr_dir);
  202.         rttosp(pcrft->cr_dir,pcrft->cr_dir);
  203.  
  204.     /* previously docked with a non-object; pstn remains the same */
  205.     } else if (pcrft->cr_dock.ip_ofst >= MAXOBJ ||
  206.     pcrft->cr_dock.ip_ofst <= 0) {
  207.         pcrft->cr_dock.ip_ptr = NULL;
  208.  
  209.     /* prevsiously docked with an object; pstn shifts to object */
  210.     } else if (pcrft->cr_dock.ip_ofst) {
  211.         pcrft->cr_dock.ip_ptr = univlst + pcrft->cr_dock.ip_ofst;
  212.         vcopy(pcrft->cr_pstn,pcrft->cr_dock.ip_ptr->uv_pstn);
  213.     }
  214.  
  215.     /* turn off inappropriate homing/autopilot */
  216.     if (pcrft->cr_auto.ip_ofst >= MAXOBJ || pcrft->cr_auto.ip_ofst <= 0)
  217.         pcrft->cr_auto.ip_ptr = NULL;
  218.     else
  219.         pcrft->cr_auto.ip_ptr = univlst + pcrft->cr_auto.ip_ofst;
  220.     for (i=0;i < MHOM;++i) {
  221.         if (pcrft->cr_hom[i].ip_ofst >= MAXOBJ || pcrft->cr_hom[i].ip_ofst <= 0)
  222.         pcrft->cr_hom[i].ip_ptr = NULL;
  223.         else
  224.         pcrft->cr_hom[i].ip_ptr = univlst + pcrft->cr_hom[i].ip_ofst;
  225.     }
  226.  
  227.     /* normalize direction and do rotation matrix */
  228.     fixdir(pcrft);
  229.  
  230.     /* get all subsystems */
  231.     binit((char *)&getskey,sizeof(getskey));
  232.     getskey.s_syskey = SUBSYS;
  233.     strcpy(getskey.s_plyr,plogin->ln_name);
  234.     strcpy(getskey.s_crft,getcrkey.cr_name);
  235.     dbmkey.dptr = (char *)&getskey;
  236.     dbmkey.dsize = sizeof(getskey);
  237.     for (i=0;i < MSYS;++i) {
  238.         getskey.s_type = i;
  239.         dbmdata = fetch(dbmkey);
  240.         if (dbmdata.dptr)
  241.         bcopy((char *)(pcrft->cr_sys+i),dbmdata.dptr,dbmdata.dsize);
  242.     }
  243.  
  244.     /* et al */
  245.     pcrft->cr_lgn = plogin;
  246.     pcrft->cr_lhit.ip_ptr = NULL;
  247.     plogin->ln_iomode = 's';
  248.     strcpy(plogin->ln_crft,getcrkey.cr_name);
  249.     plogin->ln_play.ip_ptr = puniv;
  250.     puniv->uv_type = 'P';
  251.     puniv->uv_pctr = pcrft->cr_htyp + '0';
  252.     puniv->uv_pstn = pcrft->cr_pstn;
  253.     puniv->uv_mass = pcrft->cr_sys[HULL].s_dmg;
  254.     puniv->uv_rad = 1;
  255.     puniv->uv_ptr.uv_crft = pcrft;
  256.     pcrft->cr_univ.ip_ptr = puniv;
  257.  
  258.     /* first player must start universe update */
  259.     if (!numpling++) firstplyr();
  260.  
  261. #ifdef DEBUG
  262.     VDBG("plinit return\n");
  263. #endif
  264.     return(1);
  265.  
  266. noplay: plogin->ln_stat = NULL;
  267.     output(plogin,'C',0,PROMPT);
  268.     output(plogin,0,0,0);
  269. #ifdef DEBUG
  270.     VDBG("plinit return\n");
  271. #endif
  272.     return(0);
  273. }
  274.